

#include "MyEnemies.h"
#include "MyEnemy.h"
#include "MyGame.h"
#include "MyShots.h"
#include "MyDefinitions.h"
#include "MyUniversal.h"
#include "MyEngine.h"

MyEnemies::MyEnemies(MyGame* _parent){	
	parent = _parent;
}
MyEnemies::~MyEnemies(){ 
		clean_enemies();
		for (int i = 0; i < E_COUNT; i++)
		unload_surface(enemySheet[i]);
#ifdef _DEBUG
	printf("destroyed enemies list\n");
#endif
}

//INIT -- load images to be used as enemies
void MyEnemies::load_imgs()
{   //Load the enemy sprite sheet; every enemy has movement sprites LEFT, RIGHT & shot sprites    
	
#ifdef _WIN32
	enemySheet[E_ROGUE] = load_image("misc\\rogue.png");
	enemySheet[E_MAGE] = load_image("misc\\mage.png");	
	enemySheet[E_NINJA] = load_image("misc\\ninja.png");
#else
	enemySheet[E_ROGUE] = load_image("misc/rogue.png");
	enemySheet[E_MAGE] = load_image("misc/mage.png");	
	enemySheet[E_NINJA] = load_image("misc/ninja.png");
#endif

    //If there was a problem in loading the button sprite sheet
	if ((!enemySheet[E_ROGUE])||(!enemySheet[E_MAGE])||(!enemySheet[E_NINJA]))
		{	
			printf("ERROR: Cannot load game images! (enemy sprites files missing?)");
			parent->demand_quit();
		}	    
}
void MyEnemies::init_enemies(){
	load_imgs();
	//fill the enemyTypeArray with enemies of different types, all the types must be from
	enemyTypeArray = new enemyType[E_COUNT];
	
	//enemyType info
	enemyTypeArray[E_ROGUE].life = 2;
	enemyTypeArray[E_ROGUE].exp = 50;
	enemyTypeArray[E_ROGUE].phaseCountMove = 8;
	enemyTypeArray[E_ROGUE].phaseDurationMove = 40;
	enemyTypeArray[E_ROGUE].phaseCountStand = 1;
	enemyTypeArray[E_ROGUE].phaseDurationStand = 1000;
	enemyTypeArray[E_ROGUE].speed_constant = 2.3;
	enemyTypeArray[E_ROGUE].dmg = 1;
	enemyTypeArray[E_ROGUE].movementPause = 550;
	enemyTypeArray[E_ROGUE].shotPause = 1000;
	enemyTypeArray[E_ROGUE].behaviour = BH_FIGHTER;
	enemyTypeArray[E_ROGUE].getToDist = int(PLAYER_W*0.4);
	enemyTypeArray[E_ROGUE].shootingWhat = ST_HIT;
	enemyTypeArray[E_ROGUE].cash_for_kill = 1;
	enemyTypeArray[E_ROGUE].w = 50;
	enemyTypeArray[E_ROGUE].h = 75;
	enemyTypeArray[E_ROGUE].code = E_ROGUE;
	enemyTypeArray[E_ROGUE].shot_emerge_x_l = 22;
	enemyTypeArray[E_ROGUE].shot_emerge_y_l = 22; 
	enemyTypeArray[E_ROGUE].shot_emerge_x_r = 22;
	enemyTypeArray[E_ROGUE].shot_emerge_y_r = 22; 

	enemyTypeArray[E_MAGE].life = 4;
	enemyTypeArray[E_MAGE].exp = 80;
	enemyTypeArray[E_MAGE].phaseCountMove = 8;
	enemyTypeArray[E_MAGE].phaseDurationMove = 150;
	enemyTypeArray[E_MAGE].phaseCountStand = 1;
	enemyTypeArray[E_MAGE].phaseDurationStand = 1000;
	enemyTypeArray[E_MAGE].speed_constant = 1.0;
	enemyTypeArray[E_MAGE].dmg = 3;	
	enemyTypeArray[E_MAGE].movementPause = 550; 
	enemyTypeArray[E_MAGE].shotPause = 1600;
	enemyTypeArray[E_MAGE].behaviour = BH_SPELLCASTER;
	enemyTypeArray[E_MAGE].getToDist = 300;
	enemyTypeArray[E_MAGE].shootingWhat = ST_FIREBALL;
	enemyTypeArray[E_MAGE].cash_for_kill = 2;
	enemyTypeArray[E_MAGE].w = 50;
	enemyTypeArray[E_MAGE].h = 75;
	enemyTypeArray[E_MAGE].code = E_MAGE;
	enemyTypeArray[E_MAGE].shot_emerge_x_l = 0;
	enemyTypeArray[E_MAGE].shot_emerge_y_l = 26; 
	enemyTypeArray[E_MAGE].shot_emerge_x_r = 30;
	enemyTypeArray[E_MAGE].shot_emerge_y_r = 26; 

	enemyTypeArray[E_NINJA].life = 7;
	enemyTypeArray[E_NINJA].exp = 200;
	enemyTypeArray[E_NINJA].phaseCountMove = 8;
	enemyTypeArray[E_NINJA].phaseDurationMove = 25;
	enemyTypeArray[E_NINJA].phaseCountStand = 1;
	enemyTypeArray[E_NINJA].phaseDurationStand = 1000;
	enemyTypeArray[E_NINJA].speed_constant = 2.0;
	enemyTypeArray[E_NINJA].dmg = 10;	
	enemyTypeArray[E_NINJA].movementPause = 500; 
	enemyTypeArray[E_NINJA].shotPause = 800;
	enemyTypeArray[E_NINJA].behaviour = BH_FIGHTER;
	enemyTypeArray[E_NINJA].getToDist = 25;
	enemyTypeArray[E_NINJA].shootingWhat = ST_HIT;  //need new shot type
	enemyTypeArray[E_NINJA].cash_for_kill = 4;
	enemyTypeArray[E_NINJA].w = 65;
	enemyTypeArray[E_NINJA].h = 83;
	enemyTypeArray[E_NINJA].code = E_NINJA;
	enemyTypeArray[E_NINJA].shot_emerge_x_l = 22;
	enemyTypeArray[E_NINJA].shot_emerge_y_l = 22; 
	enemyTypeArray[E_NINJA].shot_emerge_x_r = 22;
	enemyTypeArray[E_NINJA].shot_emerge_y_r = 22; 


	// clear the list for pointer holding
	enemySwarm = std::list<MyEnemy>();

	enemySpawnLevel = 1;
	someoneShot = 0;
	enemyUpdateCount = 0;
}
//DEINIT -- clears the whole enemy list
void MyEnemies::clean_enemies () { 	enemySwarm.clear(); }
//DRAW -- displays all the enemies on screen, moves them before
void MyEnemies::draw_enemies(int _t){
	std::list<MyEnemy>::iterator i;
	for (i = enemySwarm.begin();i != enemySwarm.end();)
			//out of screen bounds/dead --> true returned, remove
			if (i->move_enemy(_t)) {enemySwarm.erase (i++);}
			else {++i;}
}
//ADD -- inserts new enemy object into the list
void MyEnemies::add_enemy(double _x, double _y, int _type)
{	
	enemySwarm.push_back(*new MyEnemy(_x, _y, &enemyTypeArray[_type], this));
}
void MyEnemies::generate_enemy_group(int _t)
{
	enemyUpdateCount += _t;

	//allow 14 enemies to be on the screen
	if ((enemySwarm.size() < 15) && (enemyUpdateCount > ENEMY_SPAWN_TIME + oneToXRand(4000)))
	{
		double group_x = double (75 + (oneToXRand(2)-1)*666); 
		double group_y = 450; 

		int num_of_e = oneToXRand(4);
		bool spellcaster = false;
		int space = oneToXRand(50);

		while(true)
			if (num_of_e--) 
				add_enemy(group_x + (oneToXRand(3)-2)*space, group_y + (oneToXRand(3)-2)*space, oneToXRand(enemySpawnLevel)-1);
			else
			{
				enemyUpdateCount = 0;
				return;
			}	
	}
}
